﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using PropertyManagement.Core.DataAccess;
using PropertyManagement.Core.Model;

namespace PropertyManagement.Core {
    public class SystemUserManage {
      

        public SystemUserManage() {
        }

        public LoginStatus Login( string username,string password) {
            //throw new NotImplementedException( );
            if (string.IsNullOrEmpty(username)) {
                return LoginStatus.NoUser;
            }
            if (string.IsNullOrEmpty(password)) {
                return LoginStatus.InvalidPassword;
            }
            var user = new SystemUser {UserName = username};
            user = SystemUserDataAccess.GetByPKs(user);
            if (user == null || !user.IsValid) return LoginStatus.NoUser;
            if (SystemUser.MD5Pwd(password) == user.Password) {
                return LoginStatus.Success;
            }
            return LoginStatus.InvalidPassword;
        }

        public bool IsExisted(string username) {
            var user = new SystemUser() {
                UserName = username
            };
            //throw new NotImplementedException( );
            return SystemUserDataAccess.ExistsByPKs(user);
        }

        public bool Add(string username,string password,RightLevel level,bool isValid = true) {
            int userId = StaticProvider.Instance.CurrentLoginedUser != null
                             ? StaticProvider.Instance.CurrentLoginedUser.SystemUserID
                             : -1;
            var user = new SystemUser() {
                UserName = username,
                Password = SystemUser.MD5Pwd(password),
                RightLevel = level,
                IsValid = isValid,
                CreationDate = DateTime.Now,
                LastUpdate = DateTime.Now,
                CreationUserID = userId,
                LastActionUserID = userId,
                PasswordAnswer1 = "",
                PasswordAnswer2 = "",
                PasswordAnswer3 = "",
                PasswordQuestion1 = "",
                PasswordQuestion2 = "",
                PasswordQuestion3 = ""
            };
            //throw new NotImplementedException( );
            if (!SystemUserDataAccess.ExistsByPKs(user)) {
                SystemUserDataAccess.Add(user);
                return true;
            }
            return false;
        }

        public void Remove(string username ) {
            //throw new NotImplementedException( );
            if (!string.IsNullOrEmpty(username)) {
                var user = SystemUserDataAccess.GetByPKs(new SystemUser { UserName = username });
                if (user != null) {
                    user.IsValid = false;
                    SystemUserDataAccess.Update(user);
                    //SystemUserDataAccess.Remove(user);
                }
            }
        }

        public void Revert(string username) {
            if (!string.IsNullOrEmpty(username)) {
                var user = SystemUserDataAccess.GetByPKs(new SystemUser { UserName = username });
                if (user != null) {
                    user.IsValid = true;
                    SystemUserDataAccess.Update(user);
                    //SystemUserDataAccess.Remove(user);
                }
            }
        }

        public SystemUser Load(string username) {
            //throw new NotImplementedException( );
            if (!string.IsNullOrEmpty(username)) {
                return SystemUserDataAccess.GetByPKs(new SystemUser { UserName = username });
            }
            return null;
        }

        public void Update(SystemUser user) {
            //throw new NotImplementedException( );
            if (user.SystemUserID != 0) {
                int userId = StaticProvider.Instance.CurrentLoginedUser != null
                            ? StaticProvider.Instance.CurrentLoginedUser.SystemUserID
                            : -1;
                user.LastUpdate = DateTime.Now;
                user.LastActionUserID = userId;
                SystemUserDataAccess.Update(user);
            }
        }

    }
}
